const { LittleIO } = require('./littleIO.js');
const { Parser } = require('./parser.js');
const { CodeWriter } = require('./codeWriter.js');

class VmTranslator {
  constructor(parser, codeWriter, io) {
    this.parser = parser;
    this.codeWriter = codeWriter;
    this.io = io;
  }

  run() {
    this.io.loadFile();
    while(this.parser.hasMoreLines() && this.parser.advance()) {
      if (this.parser.isPushOrPopCommand()) {
        this.codeWriter.writePushPop(this.parser.commandType(), this.parser.arg1(), this.parser.arg2());
      } else {
        this.codeWriter.writeArithmatic(this.parser.arg1());
      }
    }
    this.codeWriter.close();
    this.io.writeToFile(this.codeWriter.cache);
  }
}

const io = new LittleIO();
const translator = new VmTranslator(new Parser(io), new CodeWriter(io), io);
translator.run();
